package br.com.gbvbahia.commons.ws;

import java.io.IOException;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public abstract class WSCommons {

	/**
	 * Cria o envelope e realiza a chamada ao WS utilizando AndroidHttpTransport
	 * Com o envelope criado voce recupera a resposta
	 * 
	 * @param version SoapEnvelope.VER11 ou SoapEnvelope.VER12...
	 * @param soap
	 * @return
	 * @throws XmlPullParserException 
	 * @throws IOException
	 */
	public SoapSerializationEnvelope getEnvelope(int version, SoapObject soap, String URL, String SOAP_ACTION) throws IOException, XmlPullParserException{
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(version);
		envelope.setOutputSoapObject(soap);
		HttpTransportSE transport = new HttpTransportSE(URL);
		transport.call(SOAP_ACTION, envelope);
		return envelope;
	}
	
	/**
	 * Cria um parametro do tipo passado para o WS que sera embitudo dentro do soap passado.
	 * @param value
	 * @param nome
	 * @param soap
	 */
	public void makeProperty(Object value, String nome, SoapObject soap, Object type) {
		PropertyInfo propPerfil = new PropertyInfo();
		propPerfil.setName(nome);
		propPerfil.setType(type);
		propPerfil.setValue(value);
		soap.addProperty(propPerfil);
	}
}
